package com.hp.controller;

import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.UUID;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.multipart.MultipartFile;

import com.hp.model.Order;
import com.hp.model.Types;
import com.hp.model.Admins;
import com.hp.service.OrderService;
import com.hp.service.TopService;
import com.hp.service.TypesService;
import com.hp.service.AdminService;
import com.hp.service.GoodsService;

@Controller
@RequestMapping("/admin")
public class AdminController {
	
	@Autowired
	private AdminService uservice;
	
	@Autowired
	private OrderService oservice;
	
	@Autowired
	private TypesService tservice;

	@Autowired
	private TopService topservice;
	
	@Autowired
	private GoodsService gservice;

	@RequestMapping("/login")
	public String login(HttpSession session,String username,String password) {
		
		
		Admins user=uservice.login(username, password);
		
		if(user!=null) {
			
			session.setAttribute("user", user);
			
			return "redirect:index.jsp";
			
		}else {
			return "login.jsp";
		}
		
	}
	
	@RequestMapping("/index")
	public String  index() {
		return "index.jsp";
	}
	
	//退出
	@RequestMapping("logout")
	 public String logout(HttpSession session) {
		
		
	     return "login.jsp";
	 }
	
	

	//显示全部,分页查询
	@RequestMapping("orderList")
	public String orderList(Model model,Integer page) {
		
		//List<Order> orderList=oservice.findAllOrder();
		
				//每页数量
				int pageSize=6;		
				
				//获取总页数
				int count=oservice.count();
				
				//计算总页数
				int pages=count/pageSize;
				if(count%pageSize!=0) {
					pages+=1;
				}
				
				//页码数
				int curPage=1;
				
				if(page!=null) {
					curPage=page;
				}
				
				//超出范围
				if(curPage>pages) {
					curPage=pages;
				}
				if(curPage<1) {
					curPage=1;
				}
		
		List<Order> orderList=oservice.findAllOrder();
		
		model.addAttribute("orderList", orderList);
		model.addAttribute("curPage", curPage);
		model.addAttribute("pages", pages);
		
		return "order_list.jsp";
	}
		
	//查询未付款的
	@RequestMapping("nopay")
	public String nopay(Model model) {	
		
		List<Order> orderList=oservice.nopayOrder();
		
		model.addAttribute("orderList", orderList);
		
		return "order_list.jsp";
	}
	
	//查询已付款
	@RequestMapping("pay")
	public String pay(Model model) {	
		
		List<Order> orderList=oservice.payOrder();
		
		model.addAttribute("orderList", orderList);
		
		return "order_list.jsp";
	}
	
	//查询配送中的
	@RequestMapping("ordering")
	public String ordering(Model model) {	
		
		List<Order> orderList=oservice.Ordering();
		
		model.addAttribute("orderList", orderList);
		
		return "order_list.jsp";
	}
	
	//查询已完成的
	@RequestMapping("finishOrder")
	public String finishOrder(Model model) {	
		
		List<Order> orderList=oservice.finishOrder();
		
		model.addAttribute("orderList", orderList);
		
		return "order_list.jsp";
	}
	
	//删除
	@RequestMapping("delOrder")
	public String delOrder(Integer id) {
		
		oservice.delorder(id);
		
		return "redirect:orderList";
	}
	
	//发货
	@RequestMapping("orderUpdate")
	public String orderUpdate(Integer id) {
		
		oservice.orderUpdate(id);
		
		return "redirect:orderList";
	}
	
	//完成
	@RequestMapping("completeOrder")
	public String completeOrder(Integer id) {
		
		oservice.completeorder(id);
		
		return "redirect:orderList";
	}
	
	
	//类目管理
	@RequestMapping("typeList")
	public String typeList(Model model) {
		
		List<Types> typeList= tservice.findAlltypes();
		model.addAttribute("typeList", typeList);
		
		return "type_list.jsp";
	}
	
	//跳转至添加
	@RequestMapping("type_add")
	public String type_add() {
		return "type_add.jsp";
	}
	
	//添加类目
	@RequestMapping("typeSave")
	public String typeSave(Types type) {
		tservice.addtype(type);
		return "redirect:typeList";
	}
	
	//通过id查找类目
	@RequestMapping("typeEdit")
	public String typeEdit(Model model, Integer id) {
		Types type=tservice.findtype(id);
		model.addAttribute("type", type);
		return "type_edit.jsp";
	}
	
	//修改类目
	@RequestMapping("typeUpdate")
	public String typeUpdate(Types type) {
		tservice.edittype(type);
		return "redirect:typeList";
	}
	
	//删除类目
	@RequestMapping("typeDelete")
	public String typeDelete(Integer id) {
		tservice.deltype(id);
		return "redirect:typeList";
	}
	
	//文件上传
	@RequestMapping("upload")
	public String upload(MultipartFile file) {
		String filename=file.getOriginalFilename();//获取文件名字
		
		System.out.println(filename);
		
		//获取文件后缀
		int lastIndex=filename.lastIndexOf(".");//找到最后一个.的位置
		String suffix=filename.substring(lastIndex);//获取文件后缀
		
		System.out.println(suffix);
		
		
		String name=UUID.randomUUID().toString().replace("-", "");//随机生成不重复文件名
		
		System.out.println(name);
		
		//新的文件名
		String newfilename=name+suffix;
		System.out.println(newfilename);
		
		//把文件存入项目目录  设置文件绝对路径
		String path="D:\\spring eclipse\\eclipse\\smsc\\WebContent\\upload1";
		String filepath=path+"\\"+newfilename;
		System.out.println(filepath);
		
		String savapath="..\\upload\\"+newfilename;//数据库中存入的路径
		
		File newfile=new File(filepath);
		
		//把上传的文件存入新文件夹 file.transferTo(newfile);报错添加try-catch
		try {
			file.transferTo(newfile);
		} catch (IllegalStateException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		
		return "";
		
	}
}
